package net.samsb.tool;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Destiny_hao on 2018/3/21.
 */

public class Tools {
    /***
     * 获取格式时间
     *
     * @param formatStr
     * @return
     */
    public static String getSystemTimeByHMS(String formatStr) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }

    /***
     * 获取星期
     *
     * @return
     */
    public static String getWeek() {
        Calendar cal = Calendar.getInstance();
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    /***
     * 截取url文件名
     * @param url
     * @return
     */
    public static String getFileName(String url) {

        String suffixes = "avi|mpeg|3gp|mp3|mp4|wav|jpeg|gif|jpg|png|apk|exe|pdf|rar|zip|docx|doc";

        Pattern pat = Pattern.compile("[\\w]+[\\.](" + suffixes + ")");//正则判断
        Matcher mc = pat.matcher(url);//条件匹配

        String substring = null;

        while(mc.find()){
            substring = mc.group(); //截取文件名后缀名
        }

        assert substring != null;
        int dot = substring.lastIndexOf('.');

        return substring.substring(0, dot);
    }

    public static Bitmap inputStreamToBitmap(InputStream inputStream) {
        return BitmapFactory.decodeStream(inputStream);
    }

    public static Bitmap bytesToBitmap(byte[] picBytes) {
        return BitmapFactory.decodeByteArray(picBytes, 0, picBytes.length);
    }

    /**
     * Toast
     *
     */
    public static void Toast(Activity activity, String string) {

        Toast.makeText(activity, string, Toast.LENGTH_SHORT).show();
    }

    /**
     * Toast
     *
     */
    public static void Toast(String string) {

        Toast.makeText(MyActivityManager.getInstance().getCurrentActivity(), string, Toast.LENGTH_SHORT).show();
    }

    public static void main(String[] arss) {
        System.out.print(getFileName("http://samsb.eiscp.net/images/d1/3.png"));
    }
}
